import axios from 'axios'
import axiosUtil from "@/services/axiosUtil"
import favoriteAPI from "@/services/favorite/FavoriteAPI"

// 创建 axios 的实例
let axiosInst = axios.create({
    timeout: 3000
})

axiosUtil.configInterceptors(axiosInst)
let req = axiosUtil.packAPI(favoriteAPI, axiosInst)

/*let req = {}
for (const k in favoriteAPI) {
    let api = favoriteAPI[k]
    /!*
        params, // 请求参数 get、delete：url；put、post、patch：data
        pathParams = [] // 路径 url
        isFormData = false, // 标记是否为 form data 数据
        config = {} // 配置参数
     *!/
    req[k] = async function (params, pathParams, isFormData = false, config = {}) {
        let newParams
        let url = api.url
        // content-type 是否是 form data 的判断
        if (params && isFormData) {
            newParams = new FormData()
            for (let p in params)
                newParams.append(p, params[p])
        } else {
            newParams = params
        }

        // 将路径参数加到 url path
        if (pathParams && pathParams.length > 0) {
            for (let i = 0; i < pathParams.length; i++)
                url += ('/' + pathParams[i])
        }

        let response = {}
        // 不用请求方法的枚举
        let method = api.method.toLowerCase()
        switch (method) {
            case 'get' :
            case 'delete' :
                config.params = newParams
                try {
                    // axios 里定义的 method 都是小写的
                    response = await axiosInst[method](url, config)
                } catch (err) {
                    response = err
                }
                break
            case 'post' :
            case 'put' :
            case 'patch' :
                try {
                    response = await axiosInst[method](url, newParams, config)
                } catch (err) {
                    response = err
                }
                break
            default:
        }
        return response
    }
}*/

export default req